// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: GHZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//

private alias object.value0 : object.timer
private alias object.value1 : Text.Line

private alias object.value2 : object.drawX
private alias object.value3 : object.drawY
private alias object.value4 : object.letterID
private alias object.value5 : object.textamount
private alias object.value6 : object.boxYpos

private alias object.value7 : Box.CurrentYpos
private alias object.value8 : Char.IsTalking

public value BoxPositionY = 0;
public value CharacterTalking = 0;
public value TextActive = 0;
public value CutsceneActive = 0;

public value Delta_Activate = 0;
public value SlenderSpared = 0;

private alias object[4].value4 : Crafting.State
public alias saveRAM[286] : saved.language

// Property Value
private alias 0 : TEXTBOX_AWAITCRAFT_0
private alias 1 : TEXTBOX_SOMETHINGAPPEAR_1
private alias 2 : UFO_CONTENTS_TIME

reserve function TextDrawing

function TextDrawing
	temp0 = object.drawX
	temp1 = object.textamount
	while temp1 > 0
	// Get the actual character at the current char pos
	GetTextInfo(object.letterID, 1, TEXTINFO_TEXTDATA, Text.Line, ArrayPos0)

	Object.Frame = 0
	
	// Take uppercase letters and turn them into a matching Sprite Frame
	if object.letterID > 64
		if object.letterID < 91
			Object.Frame  = object.letterID
			Object.Frame -= 64
		end if
	end if
	
	// Take lowercase letters and turn them into a matching Sprite Frame
	if object.letterID > 96
		if object.letterID < 123
			Object.Frame  = object.letterID
			Object.Frame -= 96
		end if
	end if

		//ASCII Table required!
		switch object.letterID
			case 33	//!
				DrawSpriteScreenFX(37, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break		
			case 63	//?
				DrawSpriteScreenFX(38, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break					
			case 42
				DrawSpriteScreenFX(27, FX_INK, object.drawX, object.drawY)
				ArrayPos0++
				temp1--
				object.drawX += 0x8
				break
			case 42
				// Notably, the space character (32) is skipped over here, as it shouldn't draw anything
				

				DrawSpriteScreenFX(27, FX_INK, object.drawX, object.drawY)
				

				// Move to the next letter
				ArrayPos0++
				
				// And decrease the remaining letter count, as well
				temp1--
				
				// Each letter should be spaced 8 pixels apart
				object.drawX += 0x8
				break
			case 64	//@	LINE SKIPPER
				object.drawY += 16
				if Char.IsTalking == 0
					object.drawX = temp0
				else
					temp0 += 8
					object.drawX = temp0
				end if

				// Move to the next letter
				ArrayPos0++
				
				// And decrease the remaining letter count, as well
				temp1--
			break
			default
			// Notably, the space character (32) is skipped over here, as it shouldn't draw anything
			

			DrawSpriteScreenFX(Object.Frame, FX_INK, object.drawX, object.drawY)

			// Move to the next letter
			ArrayPos0++
			
			// And decrease the remaining letter count, as well
			temp1--
			
			// Each letter should be spaced 8 pixels apart
			object.drawX += 0x8
			break
		end switch
	loop
end function

//if being stupid was a job

event ObjectMain
	switch object.state
	case TEXTBOX_AWAITCRAFT_0	//activation
		object.alpha = 0
		Text.Line = 0
		TextActive = 1
		if object[4].value0 == 0
			lamppostID = 1
			StopMusic()
			object.state++
		end if
	break	
	case TEXTBOX_SOMETHINGAPPEAR_1	
		Text.Line = 0
		TextActive = 1	
		HUDEnabled = 0
		object.alpha += 8
		object.timer++
		object.textamount = 0
		if object.timer == 30
			object.alpha = 255
			object.timer = 0
			object.state++
		end if
	break
	case 2
		GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
		if object.textamount == temp0
			object.timer++
			if object.timer == 150
				object.timer = 0
				object.state++
			end if
		else
			object.textamount++
			PlaySfx(SfxName[Text], false)
		end if
	break
	case 3
		object.textamount = 0
		if object.alpha > -1
			object.alpha -= 8
		else
			object.alpha = 0 			
		end if	

		if crafting.state == 1
			object.alpha = 255
			Text.Line++
			object.state++
		end if
	break
	case 4
	if crafting.state == 1
		GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
		if object.textamount == temp0
			
		else
			object.textamount++
			PlaySfx(SfxName[Text], false)
		end if
	else
		object.alpha = 0	
		object.textamount = 0
		if crafting.state == 8
			Text.Line++
			Box.CurrentYpos = 1
			Char.IsTalking = 1
			object.alpha = 255
			object.state++
		end if
	end if	
	break
	case 5
	if Text.Line < 8
		GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
		if object.textamount == temp0
			object.timer = 0
			CheckEqual(inputpress[2].start, 1)
			temp0 = checkResult
			CheckEqual(inputpress[2].buttona, 1)
			temp0 |= checkResult				
			if temp0 == 1
				Text.Line++
				object.textamount = 0
			end if
		else
			object.timer++
			if object.timer == 2
				object.textamount++
				PlaySfx(SfxName[SlenderTalk], false)
				object.timer = 0
			end if
		end if
	else
		TextActive = 0
		CutsceneActive = 0
		object.textamount = 0
		object.state++
	end if
	break
	case 6
		if object[6].state == 3
			TextActive = 1
			object.textamount = 0	
			object.state++
		end if
	break	
	case 7	//boss text
	if Text.Line < 10
		GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
		if object.textamount == temp0
			object.timer++
			if object.timer == 160
				Text.Line++
				object.timer = 0
				object.textamount = 0
			end if
		else
			object.timer++
			if object.timer == 2
				object.textamount++
				PlaySfx(SfxName[SlenderTalk], false)
				object.timer = 0
			end if
		end if
	else
		TextActive = 0
		CutsceneActive = 0
		object.textamount = 0
		object.state++
	end if
	break
	case 8
		//defeated
		if object[6].state == 20
			TextActive = 1
			PlayerHealth = 3
			object[2].yvel = 0x2A000
			object[2].state = 16

			Text.Line = 10	
			object.textamount = 0

			object.state++
		end if
	break
	case 9
		//only one way to settle this
		if object[2].ypos == 0
			CutsceneActive = 1
		end if	

		if Text.Line < 15
			GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
			if object.textamount == temp0
				object.timer = 0
				CheckEqual(inputpress[2].start, 1)
				temp0 = checkResult
				CheckEqual(inputpress[2].buttona, 1)
				temp0 |= checkResult				
				if temp0 == 1
					Text.Line++
					object.textamount = 0
				end if
			else
				object.timer++
				if object.timer == 2
					object.textamount++
					PlaySfx(SfxName[SlenderTalk], false)
					object.timer = 0
				end if
			end if
		else
			TextActive = 0
			CutsceneActive = 1
			object.textamount = 0
			Delta_Activate = 1
			object.state++
		end if
	break	
	case 10
		if Delta_Activate == 0
			if SlenderSpared == 0	//no spare
				Text.Line = 28
			else
				Text.Line = 22	//spare!
			end if
			TextActive = 1
			CutsceneActive = 1
			object.textamount = 0
			object.state = 11	
			object.timer = 0
		end if
		
		//legacy stuff
		// if object[6].state == 23
		// 	TextActive = 1
		// 	object.timer = 0
		// 	Text.Line = 15
		// 	object.state++
		// end if

		// if object[6].state == 25
		// 	TextActive = 1
		// 	object.timer = 0
		// 	Text.Line = 17
		// 	object.state = 14
		// end if		
	break
	case 11
		if SlenderSpared == 1
			if Text.Line < 27
				GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
				if object.textamount == temp0
					CheckEqual(inputpress[2].start, 1)
					temp0 = checkResult
					CheckEqual(inputpress[2].buttona, 1)
					temp0 |= checkResult				
					if temp0 == 1
						object.timer = 0
						Text.Line++
						object.textamount = 0
					end if
				else
					object.timer++
					if object.timer == 2
						object.textamount++
						PlaySfx(SfxName[SlenderTalk], false)
						object.timer = 0
					end if
				end if
			else
				TextActive = 0
				CutsceneActive = 0
				object.state++
			end if
		else
			if Text.Line < 32
				GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
				if object.textamount == temp0
					CheckEqual(inputpress[2].start, 1)
					temp0 = checkResult
					CheckEqual(inputpress[2].buttona, 1)
					temp0 |= checkResult				
					if temp0 == 1
						object.timer = 0
						Text.Line++
						object.textamount = 0
					end if
				else
					object.timer++
					if object.timer == 2
						object.textamount++
						PlaySfx(SfxName[SlenderTalk], false)
						object.timer = 0
					end if
				end if
			else
				TextActive = 0
				CutsceneActive = 0
				if object[6].state == 23	
					text.line = 15
					TextActive = 1
					object.state = 14
				end if
			end if			
		end if
	break
	case 12
		object.timer++
		if object.timer == 200
			object.timer = 0
			object[2].controlmode = -1
			PlayMusic(6)
			object.state++
		end if
	break
	case 13
		object.timer++
		switch stage.playerlistpos
		case 0	//normal mode beat
			saveRAM[6901] = 1
		break
		case 1	//easy
			saveRAM[6900] = 1
		break
		case 2	//hard
			saveRAM[6902] = 1
		break
		case 5	//extreme
			saveRAM[6903] = 1
		break
		end switch


		if object.timer == 300

			WriteSaveRAM()
			stage.activeList = BONUS_STAGE
			stage.listPos = 12
			LoadStage()						
		
		end if
	break
	case 14
		if Text.Line < 18
			GetTextInfo(temp0, 1, TEXTINFO_TEXTSIZE, Text.Line, 0)
			if object.textamount == temp0
				object.timer++
				if object.timer == 150
					object.timer = 0
					Text.Line++
					object.textamount = 0
				end if
			else
				object.timer++
				if object.timer == 2
					object.textamount++
					PlaySfx(SfxName[SlenderTalk], false)
					object.timer = 0
				end if
			end if
		else
			TextActive = 0
			CutsceneActive = 0
			object.textamount = 0
			object.state = 12
		end if
	break
	end switch
end event

event ObjectDraw
	object.inkEffect = INK_ALPHA

	ArrayPos0 = 0	//starting letter



	if TextActive == 1

		if Box.CurrentYpos == 0
		
			object.drawY = 35
			
			temp1 = screen.xcenter

			DrawSpriteScreenFX(36, FX_INK, temp1, object.drawY)

		else

			object.drawY = 180

			object.boxYpos = object.drawY
			object.boxYpos -= 25

			temp1 = screen.xcenter
			DrawSpriteScreenFX(36, FX_INK, temp1, object.drawY)

			if Char.IsTalking == 1
				temp1 += 36
				DrawSpriteScreenFX(35, FX_INK, temp1, object.drawY)
			end if	

		end if

		if Char.IsTalking == 1
			temp3 = screen.xcenter
			temp3 -= 64
			object.drawX = temp3
		else
			temp3 = screen.xcenter
			temp3 -= 138
			object.drawX = temp3
		end if

		CallFunction(TextDrawing)
	end if	

	if object.state == 13
		DrawRect(0, 0, screen.xsize, screen.ysize, color7, color7, color7, 255)

		temp1 = screen.xcenter

		DrawSpriteScreenFX(39, FX_INK, temp1, screen.ycenter)

	end if



end event

event ObjectStartup

	LoadSpriteSheet("3D_Test/Objects.gif")
	switch saved.language
		case 0 //EN
			LoadTextFile(1, "Data/Game/Slenderman.txt")
		break
		case 1 //PTR
			LoadTextFile(1, "Data/Game/Portuguese/Slenderman.txt")
		break
		case 2 //ES
			LoadTextFile(1, "Data/Game/Spanish/Slenderman.txt")
		break
		case 3 //ES
			LoadTextFile(1, "Data/Game/French/SlendermanFR.txt")
		break		
	end switch
	
	SpriteFrame(0, 0, 0, 0, 0, 0)	//0 the bar

	// #1 -> #26 - Alphabet
	// (Using loops to automate Sprite Framing)

	//not related too stuff in objectdraw

	//temp7: starting ypos, progresses by 17 and stops before 100, because theres only 3 rows
	//temp6: starting xpos, the letter A
	temp1 = 49
	while temp1 < 100
		temp0 = 549
		while temp0 < 648
			SpriteFrame(-3, -6, 8, 16, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop

	TextActive = 0

	//undertale
	SpriteFrame(-3, -6, 8, 16, 513, 100) //27

	//undertale
	SpriteFrame(-180, -6, 65, 51, 513, 117) //35 slender man icon

	SpriteFrame(-160, -22, 320, 80, 26, 683) //36 slender man icon	
	
	//undertale
	SpriteFrame(-3, -6, 8, 16, 522, 100) //37
	
	
	//undertale
	SpriteFrame(-3, -6, 8, 16, 531, 100) //38

	//undertale
	SpriteFrame(-130, -128, 256, 256, 2, 766) //39

	// Place the HUD object into reserved object slot 4 and initialise its values
	Object[8].Type = TypeName[Message Text]
	
	// The object should always be run, regardless if it's within camera bounds or not
	Object[8].Priority = PRIORITY_ACTIVE
	
	// Make the HUD draw ontop of all other sprites, too
	Object[8].DrawOrder = 6

end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("IIZ/Objects1.gif")
	SpriteFrame(0, 0, 64, 64, 153, 154)
end event
